package com.rxt.thermopro.ui.fragment

import android.net.Uri
import android.os.Bundle
import androidx.core.app.ShareCompat
import com.app.common.base.BaseFragment
import com.app.common.viewmodel.BaseViewModel
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.widget.RulerView
import kotlinx.android.synthetic.main.fragment_ambient_temp.*
import kotlinx.android.synthetic.main.fragment_ambient_temp.backButton
import kotlinx.android.synthetic.main.fragment_ambient_temp.shareLayout
import kotlinx.android.synthetic.main.fragment_ambient_temp.viewShare

class AmbientTempFragment : BaseFragment<BaseViewModel>() {
    override fun layoutId(): Int {
        return R.layout.fragment_ambient_temp
    }

    companion object {

        fun newInstance(): AmbientTempFragment {
            return AmbientTempFragment()
        }

        private const val ARG_HIGH_VALUE = "highValue"
        private const val ARG_LOW_VALUE = "lowValue"

        fun newInstance(highValue: String, lowValue: String): AmbientTempFragment {
            return AmbientTempFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_HIGH_VALUE, highValue)
                    putString(ARG_LOW_VALUE, lowValue)
                }
            }
        }
    }

    private var lastChangeTime = 0L
    override fun initView(savedInstanceState: Bundle?) {
        var highValue = arguments?.getString(ARG_HIGH_VALUE, "250") ?: LocalDataDao.INSTANCE
            .getProfileHeader().highTemperatureNum
        var lowValue = arguments?.getString(ARG_LOW_VALUE, "50") ?: LocalDataDao.INSTANCE
            .getProfileHeader().lowTemperatureNum
        if (LocalDataDao.INSTANCE.getTempUnit()==LocalDataDao.TEMP_UNIT_C){
            rulerView.setUnitC()
        }else{
            highValue=LocalDataDao.INSTANCE
                .getProfileHeader().highValueF
            lowValue=LocalDataDao.INSTANCE
                .getProfileHeader().lowValueF
            rulerView.setUnitF()
        }

        rulerView.postDelayed({
            rulerView.setFmChanel(highValue.toDouble())
            rulerView.setLowFmChanel(lowValue.toDouble())
        }, 500)
        initListener()
    }

    private fun initListener() {
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        viewShare.setOnClickListener {
            val uri = shareLayout.saveImage()
            uri?.let {
                val type = "image/*"

                startActivity(
                    ShareCompat
                        .IntentBuilder
                        .from(requireActivity())
                        .setType(type)
                        .setStream(Uri.parse(uri)).intent
                )
            }
        }

        rulerView.setOnChangerListener(object : RulerView.OnChangerListener {
            override fun onValueChange(highValue: Double, lowValue: Double) {
                LocalDataDao.INSTANCE.updateProfileHeader(
                    highValue.toInt().toString(),
                    lowValue.toInt().toString()
                )
            }
        })
    }
}